Thread: struct mystruct mystruct = { item1: value, . . .

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    struct mystruct mystruct = { item1: value, . . .

    Hmm, something else I didn't know -- is _THIS_ in the standard?
    Code:
    struct mystruct {
            int item1, item2, item3, item4, item5, item10; // I think you get the point
    } mystruct = {
            item1: 1,
            item2: 2,
            item5: 5,
            item4: 4,
    };
    Now, using the above struct definition, I have done this many times in the past:
    Code:
    struct mystruct mystruct = {
            .item1 = 1,
            .item2 = 2,
    };
    but the use of the colon is something I've never seen before. It this compiler dependent?


    Thanks again!

    Man, you think you know a language fairly well, then all sorts of new things start happening.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's called a bit field.
    If you understand what you're doing, you're not learning anything.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A bit field would be this:
    Code:
    struct foo
    {
        type name:size;
    };
    His use of : is in the initializer.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    At first, I thought Quzah was on to something, that item1: is being treated as a label. But then I wrote this sample program to test it out. Run it and look at the output. It initializes the struct properly.
    Code:
    #include <stdio.h>
    
    struct mystruct {
        int item1, item2, item3, item4, item5, item10; // I think you get the point
    } mystruct = {
           item1: 1,
           item2: 2,
           item5: 5,
           item4: 4,
    };
    
    int main(void)
    {
        printf("mystruct:\n");
        printf("\titem1  %d\n", mystruct.item1);
        printf("\titem2  %d\n", mystruct.item2);
        printf("\titem3  %d\n", mystruct.item3);
        printf("\titem4  %d\n", mystruct.item4);
        printf("\titem5  %d\n", mystruct.item5);
        printf("\titem10 %d\n", mystruct.item10);
    
        return 0;
    }
    It compiled with "gcc -Wall main.c" just fine. Then I compiled it with "gcc -Wall -pedantic main.c" and got the following errors
    main.c:6: warning: obsolete use of designated initializer with ‘:’
    main.c:7: warning: obsolete use of designated initializer with ‘:’
    main.c:8: warning: obsolete use of designated initializer with ‘:’
    main.c:9: warning: obsolete use of designated initializer with ‘:’

    Looks like some outdated syntax.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    struct mystruct mystruct = {
            .item1 = 1,
            .item2 = 2,
    };
    http://publib.boulder.ibm.com/infoce...esignators.htm
    For the first one, i don't think it's standard.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Using .initializers is C99.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    We do have a sticky thread ....
    C Draft Standards
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I compiled it with PellesC....
    Code:
    Building main.obj.
    D:\Programming\Experiments\dosprog\main.c(9): error #2048: Undeclared identifier 'item1'.
    D:\Programming\Experiments\dosprog\main.c(9): error #2001: Syntax error: expected '}' but found ':'.
    D:\Programming\Experiments\dosprog\main.c(13): error #2069: Initializer must be constant.
    *** Error code: 1 ***
    Done.
    It's just bad code....

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    Code:
    struct mystruct {
        int item1, item2, item3, item4, item5, item10; // I think you get the point
    } mystruct = {
           item1: 1,
           item2: 2,
           item5: 5,
           item4: 4,
    };
    you can define a struct like:
    Code:
    struct mystruct {
           item1: 1,
           item2: 2,
           item5: 5,
           item4: 4,
    };
    can't initialize it that way

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Salem View Post
    We do have a sticky thread ....
    C Draft Standards
    Doh! I forgot about that.

    Didn't, however, find anything about initialization of structure elements using ':'. In fact, the only place I found ':' mentioned was under the labels section.

    Based upon the compiler error mention above, however, it appears to be something from the original language. That being said, I'm changing it to the latest format for init.

    Thanks for the comments!

  11. #11
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Jesius View Post
    Code:
    struct mystruct {
        int item1, item2, item3, item4, item5, item10; // I think you get the point
    } mystruct = {
           item1: 1,
           item2: 2,
           item5: 5,
           item4: 4,
    };
    you can define a struct like:
    Code:
    struct mystruct {
           item1: 1,
           item2: 2,
           item5: 5,
           item4: 4,
    };
    can't initialize it that way
    You are wrong. I can init it this way using GCC. In fact, either way works on any of the GCC's that I have (I have 5).

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Kennedy View Post
    You are wrong. I can init it this way using GCC. In fact, either way works on any of the GCC's that I have (I have 5).
    Do you have your errors and warnings turned off?

    Message 1 is absolutely not standard C .... in fact I'm surprised GCC will compile it, PellesC won't and it's pretty big on standards.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It looks like a GCC extension: GCC's website
    In a structure initializer, specify the name of a field to initialize with `fieldname:' before the element value. For example, given the following structure,

    Code:
      struct point { int x, y; };
    the following initialization

    Code:
      struct point p = { y: yvalue, x: xvalue };
    is equivalent to

    Code:
      struct point p = { xvalue, yvalue };
    If you understand what you're doing, you're not learning anything.

  14. #14
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by CommonTater View Post
    Do you have your errors and warnings turned off?

    Message 1 is absolutely not standard C .... in fact I'm surprised GCC will compile it, PellesC won't and it's pretty big on standards.
    The build system for the kernel enables _ALL_ warnings and treats all warnings as errors (at least with 2.6.36.1 this is the case -- This is something that Linus has been pushing for for many years, though I don't know when he started the warnings as errors bit and why you can see many Linus melt downs on LKML if you search for "warning bomb")

  15. #15
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by itsme86 View Post
    It looks like a GCC extension: GCC's website
    Hmmm. Something seems strange to me. The code that I'm looking at is an init of the fsops struct. I know that llseek is first, though llseek was not configured in this init, but the init does work. I have even tried it with a simple helloworld and it does what it is "supposed" to do. . .

    Like I said, I'm changing this code since it is, at a minimum, confusing.

    Andy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Realloc()
    By krazyxazn in forum C Programming
    Replies: 10
    Last Post: 01-27-2010, 10:05 PM
  2. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM